package com.hua.demo01;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

//实现多线程同步下载图片
public class TestThread2 extends Thread {
    private String url;
    private String name;
    public TestThread2(String url,String name){

        this.url= url;
        this.name = name;
    }



    @Override
    public void run() {
        WebDownLoader webDownLoader = new WebDownLoader();
        webDownLoader.downloader(url,name);
        System.out.println("下载文件名为："+name);



    }


    public static void main(String[] args) {
        TestThread2 testThread1 = new TestThread2("http://sc.cltt.org/Web/common/GeneratePhotoByStuID.ashx?StuID=5101621120079", "1.jpg");
        TestThread2 testThread2 = new TestThread2("https://blog.kuangstudy.com/usr/themes/handsome/usr/img/sj/2.jpg", "2.jpg");
        TestThread2 testThread3 = new TestThread2("https://blog.kuangstudy.com/usr/themes/handsome/usr/img/sj/3.jpg", "3.jpg");
        TestThread2 testThread4 = new TestThread2("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fattach.bbs.miui.com%2Fforum%2F201409%2F09%2F130415ome6e930vp99to05.jpg&refer=http%3A%2F%2Fattach.bbs.miui.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1624413812&t=d89742c15d4e4b1f6511755023930dab", "4.jpg");
        //不是顺序执行，而是同时执行
        testThread1.start();
//        testThread2.start();
//        testThread3.start();
        testThread4.start();




    }
}


//下载器
class WebDownLoader{

    public void downloader(String url,String name){
        try {
            FileUtils.copyURLToFile(new URL(url),
                    new File(name));
        }catch (IOException e ){
            e.printStackTrace();
            System.out.println("IO异常,downloader方法出现问题");
        }

    }




}